<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
<meta charset="utf-8">
<meta name="renderer" content="webkit|ie-comp|ie-stand">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link rel="stylesheet" type="text/css" href="${ctx}/static/plug/h-ui/css/H-ui.min.css" />
<link rel="stylesheet" type="text/css" href="${ctx}/static/plug/Hui-iconfont/1.0.8/iconfont.min.css" />
<link rel="stylesheet" type="text/css" href="${ctx}/static/plug/zTree_v3/css/demo.css" />
<link rel="stylesheet" type="text/css" href="${ctx}/static/plug/zTree_v3/css/zTreeStyle/zTreeStyle.css" />
<style type="text/css">
.row {
	margin-top: 15px
}
</style>
<title>角色信息</title>
</head>
<body>
	<div class="panel panel-default">
		<div class="panel-body">
			<form id="dataForm">
				<div class="row cl">
					<input type="hidden" value="${bmsRole.id}" id="id" name="id"></input>
					<label class="form-label col-xs-2"><span class="c-red">*</span>角色名称：</label>
					<div class="form-label col-xs-4">
						<input class="input-text required" type="text" value="${bmsRole.name}" id="name" name="name"></input>
					</div>
				</div>
				
				<div class="row cl">
					<label class="form-label col-xs-2"><span class="c-red">*</span>角色类型：</label>
					<div class="form-label col-xs-4">
						<select id="type">
							<option value="0">公共角色</option>
							<option value="1">平台角色</option>
							<option value="2">景区角色</option>
						</select>
					</div>
				</div>
				
				<div class="row cl">
					<label class="form-label col-xs-2"><span class="c-red">*</span>权限设置：</label>
					<div class="form-label col-xs-4">
						<div class="zTreeDemoBackground left">
							<ul id="treeDemo" class="ztree"></ul>
						</div>
					</div>
					
					
				</div>
				<div class="row cl" style="text-align: center">
					<button id="updateBtn" class="btn btn-secondary radius size-L" type="button">${bmsRole == null ? "保存":"修改"}</button>
					<button id="cancelBtn" class="btn btn-secondary radius size-L" type="button">取消</button>
				</div>
			</form>
		</div>
	</div>
	<script type="text/javascript" src="${ctx}/static/plug/zTree_v3/js/jquery.ztree.core.js"></script>
	<script type="text/javascript" src="${ctx}/static/plug/zTree_v3/js/jquery.ztree.excheck.js"></script>
	<script type="text/javascript">
		$(function() {
			var type = '${bmsRole.type}';
			if(type != null && type !='') {
				$("#type > option[value=" + type + "]").attr("selected","selected");
			}
			
			var roleId = $("#id").val();
			$.post("${ctx}/role/menuTreeList", {"roleId" : roleId}, function(data) {
				console.log(data.bmsZTreeResourceVo)
				var setting = {
					check: {
						enable: true
					},
					data: {
						simpleData: {
							enable: true,
							idKey: "id"
						}
					},
				};

				var zNodes = data.bmsZTreeResourceVo;
				setting.check.chkboxType = { "Y" : "ps", "N" : "ps" };

				$.fn.zTree.init($("#treeDemo"), setting, zNodes);
				console.log($.fn.zTree.getZTreeObj("treeDemo").getNodes());
				
			});
			
			
			
			
		});
		
	
	
		var canSubmit = true;
		$(function() {
			$("#updateBtn").click(function() {
				if(canSubmit) {
					canSubmit = false;
					var updateData = new Object();
					updateData.id = $("#id").val();
					updateData.type = $("#type").val();
					updateData.name = $("#name").val();
					
					if(updateData.name.length == 0) {
						layer.msg("请输入姓名!");
						canSubmit = true;
						return;
					}
					var menuTreeVoList = new Array();
					for (var i = 0; i < $.fn.zTree.getZTreeObj("treeDemo").getNodes().length; i++) {//第一级
						var treeDemos = $.fn.zTree.getZTreeObj("treeDemo").getNodes()[i];
						console.log(treeDemos);
						console.log(treeDemos.checked);
						if(treeDemos.resourceId != null && treeDemos.checked == true){
							console.log(treeDemos.resourceId);
							menuTreeVoList.push(treeDemos.resourceId);
						}
						if(treeDemos.child == false){
							for (var j = 0; j < treeDemos.children.length; j++) {//第二级
								var treeDemochildren = treeDemos.children[j];
								console.log(treeDemochildren);
								console.log(treeDemochildren.checked);
								if(treeDemochildren.resourceId != null && treeDemochildren.checked == true){
									console.log(treeDemochildren.resourceId);
									menuTreeVoList.push(treeDemochildren.resourceId);
								}
								if(treeDemochildren.child == false){
									for (var k = 0; k < treeDemochildren.children.length; k++) {//第三级
										var treeDemo2children = treeDemochildren.children[k];
										console.log(treeDemo2children);
										console.log(treeDemo2children.checked)
										if(treeDemo2children.resourceId != null && treeDemo2children.checked == true){
											console.log(treeDemo2children.resourceId)
											menuTreeVoList.push(treeDemo2children.resourceId);
										}	
										if(treeDemo2children.child == false){
											for (var l = 0; l < treeDemo2children.children.length; l++) {//第四级
												var treeDemo3children = treeDemo2children.children[l];
												console.log(treeDemo3children);
												console.log(treeDemo3children.checked)
												if(treeDemo3children.resourceId != null && treeDemo3children.checked == true){
													console.log(treeDemo3children.resourceId)
													menuTreeVoList.push(treeDemo3children.resourceId);
												}	
											}
										}
									}
								}
							}
						}
					}
					console.log(menuTreeVoList);
					updateData.menuTreeVoList = menuTreeVoList;
					console.log(updateData);
					
					
					$.ajax({ 
			            type:"POST", 
			            url:"${ctx}/role/saveOrUpdateRole", 
			            traditional : true,  //发送信息至服务器时内容编码类型。             
			            dataType:"json",  // 预期服务器返回的数据类型。如果不指定，jQuery 将自动根据 HTTP 包 MIME 信息来智能判断，比如XML MIME类型就被识别为XML。
			            data:updateData, 
			            success:function(data){ 
			            	if(data.result){
								var index = parent.layer.getFrameIndex(window.name);
								parent.reloadData();
								parent.layer.close(index);
			            	}
			            } 
			         }); 
					
					
					
					
				}
			});

			$("#cancelBtn").click(function() {
				var index = parent.layer.getFrameIndex(window.name);
				parent.layer.close(index);
			});
			
		});
	</script>
</body>
</html>
